from PySide2.QtCore import QObject
from PySide2.QtCore import QThread
from PySide2.QtCore import Signal, Slot

class DrawChartWorkSignal(QObject):
    drawCompleted = Signal()

class DrawChartWork(QObject):
    def __init__(self):
        super(DrawChartWork, self).__init__()
        self.signals = DrawChartWorkSignal()

    @Slot(object)
    def do_draw(self, o):
        print(o)
        o["func"](*o["args"])
        self.signals.drawCompleted.emit()

class DrawChartSignal(QObject):
    drawCompleted = Signal()

class _DrawChartSignal(QObject):
    startChart = Signal(object)

class DrawChart(QObject):
    def __init__(self):
        super(DrawChart, self).__init__()
        self.signals = DrawChartSignal()
        self._signals = _DrawChartSignal()

        self._thread = QThread()
        self._work = DrawChartWork()
        self._work.moveToThread(self._thread)
        self._thread.finished.connect(self._work.deleteLater)
        self._signals.startChart.connect(self._work.do_draw)

        self._work.signals.drawCompleted.connect(self.signals.drawCompleted)

        self._thread.start()

    def __del__(self):
        try:
            self._thread.quit()
            self._thread.wait()
        except:
            pass

    @Slot(object)
    def do_draw(self, o):
        print (o)
        self._signals.startChart.emit(o)